clear all;clc;close all;
mex cec14_func.cpp -DWINDOWS
func_num=1;
D=10;
Xmin=-100;
Xmax=100;
pop_size=100;
iter_max=5000;
runs=50;
fhd=str2func('cec14_func');
num_IniPos = 1;

bool_Plot = true;
bool_Plot = false;

bool_PSO = false;
bool_SGSA = false;
bool_EGSA = false;
bool_QGSA = false;
bool_GWO = false;
bool_ABC = false;
bool_FCO = false;

% bool_PSO = true;
% bool_SGSA = true;
% bool_EGSA = true;
% bool_QGSA = true;
% bool_GWO = true;
% bool_ABC = true;
bool_FCO = true;

tic 
for i=1:1
    func_num=1;
    for j=1:runs
        i,j,
        inipos_filename = cd;
        inipos_filename = [inipos_filename '\IniPos\IniPos_DSMMFR_' num2str(D) '_' num2str(pop_size) '_' num2str(Xmin) '_' num2str(Xmax) '_' num2str(func_num) '_' num2str(j) '.dat'];
        if(exist(inipos_filename) ~= 0)
            pos = csvread(inipos_filename);
        else
            pos = InitializePopulation(D,pop_size,Xmin,Xmax);  
            csvwrite(inipos_filename, pos);
        end
        
        if(bool_PSO)
            [gbest,gbestval,FES,fitness,gbestval_history]= PSO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            PSO_gbest(j,:) = gbest;
            PSO_fbest(j,1) = gbestval;
            PSO_fbest(j,1)
%             gbestval_history
%             mean_fitness = mean(fitness)
%             std_fitness = std(fitness)
        end
        if(bool_SGSA)
            [gbest,gbestval,FES]= SGSA_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            SGSA_gbest(j,:) = gbest;
            SGSA_fbest(j,1) = gbestval;
            SGSA_fbest(j,1)
        end
        if(bool_EGSA)
            [gbest,gbestval,FES]= EGSA_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            EGSA_gbest(j,:) = gbest;
            EGSA_fbest(j,1) = gbestval;
            EGSA_fbest(j,1)
        end        
        if(bool_QGSA)
            [gbest,gbestval,FES]= QGSA_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            QGSA_gbest(j,:) = gbest;
            QGSA_fbest(j,1) = gbestval;
            QGSA_fbest(j,1)
        end              
        if(bool_GWO)
%             [gbest,gbestval,FES]= GWO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
%             GWO_gbest(j,:) = gbest;
%             GWO_fbest(j,1) = gbestval;
%             GWO_fbest(j,1)
            [gbest,gbestval,FES]= MGWO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            MGWO_gbest(j,:) = gbest;
            MGWO_fbest(j,1) = gbestval;
            MGWO_fbest(j,1)
        end                  
        if(bool_ABC)
            [gbest,gbestval,FES]= ABC_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            ABC_gbest(j,:) = gbest;
            ABC_fbest(j,1) = gbestval;
            ABC_fbest(j,1)
        end            
        if(bool_FCO)
            [gbest,gbestval,FES,fitness,gbestval_history]= FCO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,pos,bool_Plot,func_num);
            FCO_gbest(j,:) = gbest;
            FCO_fbest(j,1) = gbestval;
            FCO_fbest(j,1)
            gbestval_history;
%             mean_fitness = mean(fitness)
%             std_fitness = std(fitness)
        end      
    end
    func_num
    if(bool_PSO)
        PSO_mean(i)=mean(PSO_fbest(:,1))
        PSO_std(i)=std(PSO_fbest(:,1))
    end
    if(bool_SGSA)
        SGSA_mean(i)=mean(SGSA_fbest(:,1))
        SGSA_std(i)=std(SGSA_fbest(:,1))
    end
    if(bool_EGSA)
        EGSA_mean(i)=mean(EGSA_fbest(:,1))
        EGSA_std(i)=std(EGSA_fbest(:,1))
    end
    if(bool_QGSA)
        QGSA_mean(i)=mean(QGSA_fbest(:,1))
        QGSA_std(i)=std(QGSA_fbest(:,1))
    end
    if(bool_GWO)
%         GWO_mean(i)=mean(GWO_fbest(:,1))
%         GWO_std(i)=std(GWO_fbest(:,1))
        MGWO_mean(i)=mean(MGWO_fbest(:,1))
        MGWO_std(i)=std(MGWO_fbest(:,1))
    end
    if(bool_ABC)
        ABC_mean(i)=mean(ABC_fbest(:,1))
        ABC_std(i)=std(ABC_fbest(:,1))
    end
    if(bool_FCO)
        FCO_mean(i)=mean(FCO_fbest(:,1))
        FCO_std(i)=std(FCO_fbest(:,1))
    end
end
toc

% figure(8)
% hold on
% if(bool_PSO)
%     plot(PSO_fbest, 'k')
% end
% if(bool_SGSA)
%     plot(SGSA_fbest, 'r')
% end
% if(bool_EGSA)
%     plot(EGSA_fbest, 'g')
% end
% if(bool_QGSA)
%     plot(QGSA_fbest, 'b')
% end
% if(bool_GWO)
% %     plot(GWO_fbest, 'm')
%     plot(MGWO_fbest, 'y')
% end
% if(bool_ABC)
%     plot(ABC_fbest, 'm')
% end
% if(bool_FCO)
%     plot(FCO_fbest, 'y')
% end

h=figure(9)
if(bool_PSO)
    plot(PSO_mean, 'k')
    csvwrite('PSO_mean.dat',PSO_mean);
    csvwrite('PSO_std.dat',PSO_std);
end
if(bool_SGSA)
    plot(SGSA_mean, 'r')
    csvwrite('SGSA_mean.dat',SGSA_mean);
    csvwrite('SGSA_std.dat',SGSA_std);
end
if(bool_EGSA)
    plot(EGSA_mean, 'g')
    csvwrite('EGSA_mean.dat',EGSA_mean);
    csvwrite('EGSA_std.dat',EGSA_std);
end
if(bool_QGSA)
    plot(QGSA_mean, 'b')
    csvwrite('QGSA_mean.dat',QGSA_mean);
    csvwrite('QGSA_std.dat',QGSA_std);
end
if(bool_GWO)
%     plot(GWO_mean, 'm')
%     csvwrite('GWO_mean.dat',GWO_mean);
%     csvwrite('GWO_std.dat',GWO_std);
    plot(MGWO_mean, 'y')
    csvwrite('MGWO_mean.dat',MGWO_mean);
    csvwrite('MGWO_std.dat',MGWO_std);
end
if(bool_ABC)
    plot(ABC_mean, 'm')
    csvwrite('ABC_mean.dat',ABC_mean);
    csvwrite('ABC_std.dat',ABC_std);
end
if(bool_FCO)
    plot(FCO_mean, 'y')
    csvwrite('FCO_mean.dat',FCO_mean);
    csvwrite('FCO_std.dat',FCO_std);
end
saveas(h,'Result','png');
% 
% for i=1:30
% eval(['load input_data/shift_data_' num2str(i) '.txt']);
% eval(['O=shift_data_' num2str(i) '(1:10);']);
% f(i)=cec14_func(O',i);i,f(i)
% end